package com.cykj.page02;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @BelongsProject: HandTomcat
 * @BelongsPackage: com.cykj
 * @Author: 阿隆
 * @CreateTime: 2025-10-18 11:30
 * @Description: 多接受服务端
 * @Version: 1.0
 */
public class MyServer {
    public static void main(String[] args) {
        /*
         * 多发多收:
         * 服务端:   死循环 一直接受客户端的消息
         * 客户端:   死循环  一直发送消息 Scanner
         *
         * */

        try {
            //创建ServerSocket => 服务器
            ServerSocket serverSocket = new ServerSocket(9090);

            System.out.println("服务端启动端口号为: 9090");

            //等待客户端连接  阻塞
            Socket socket = serverSocket.accept();

            while (true) {
                //读取客户端发送的消息
                InputStream is = socket.getInputStream();

                byte[] bytes = new byte[1024];

                //读取
                int len = is.read(bytes);

                System.out.println(new String(bytes, 0, len));

            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
